package post.graduate.go.ready.experiment.page.parser;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

@SuppressWarnings("unchecked")
public class Parser extends ParserCallback {

	private boolean formTag = false;
	
	private boolean selectTag = false;
	
	private boolean error;
	
	private String labelInfo = "";
	
	private String selectInfo = "";
	
	private List<String> labelList = new ArrayList<String>(); 
	
	private List<String> inputList = new ArrayList<String>(); 
	
	private List<String> errorList = new ArrayList<String>();
	
	private List<List> inputLists = new ArrayList<List>(); 
	
	private List<String> formList = new ArrayList<String>();
	
	
	
	public boolean isError() {
		return error;
	}

	public List<String> getLabelList() {
		return labelList;
	}

	public List<String> getFormList() {
		return formList;
	}
	
	public List<String> getErrorList() {
		return errorList;
	}
	
	public List<List> getInputLists() {
		return inputLists;
	}
	

	@Override
	public void handleEndTag(Tag t, int pos) {
		if(t == HTML.Tag.FORM)
		{
			formTag = false;
			inputLists.add(inputList);
		} else if(formTag && selectTag && t == HTML.Tag.SELECT)
		{
			selectTag = false;
			selectInfo += "\n</select>";
			inputList.add(selectInfo);
		} else if(formTag && selectTag && t == HTML.Tag.OPTION)
		{
			selectInfo += " </option>";
		}
		labelInfo = "";
	}

	@Override
	public void handleError(String errorMsg, int pos) {
		error = true;
		errorList.add(errorMsg);
		super.handleError(errorMsg, pos);
	}

	@Override
	public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
		if(t == HTML.Tag.INPUT)
		{
			if(formTag)
			{
				String input = "<input";
				Enumeration<Attribute> attributeNames = (Enumeration<Attribute>)a.getAttributeNames();
				if(attributeNames != null)
				{
					for(;attributeNames.hasMoreElements();)
					{
						Object attributeName = attributeNames.nextElement();
						if(attributeName.getClass() == String.class)
						{
							input += " " + attributeName + "=\"" + a.getAttribute(attributeName) + "\"";
						} else if(attributeName.getClass() == Attribute.class)
						{
							input +=  " " + attributeName.toString() + "=\"" + a.getAttribute(attributeName) + "\"";
						}
					}
				}
				input += ">";
				inputList.add(input);
			}
		}
		labelInfo = "";
	}

	@Override
	public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
		if(t == HTML.Tag.FORM)
		{
			formTag = true;
			inputList = new ArrayList<String>();
			String form = "<form";
			Enumeration<Attribute> attributeNames = (Enumeration<Attribute>)a.getAttributeNames();
			if(attributeNames != null)
			{
				for(;attributeNames.hasMoreElements();)
				{
					Object attributeName = attributeNames.nextElement();
					if(attributeName.getClass() == String.class)
					{
						form += " " + attributeName + "=\"" + a.getAttribute(attributeName) + "\"";
					} else if(attributeName.getClass() == Attribute.class)
					{
						form +=  " " + attributeName.toString() + "=\"" + a.getAttribute(attributeName) + "\"";
					}
				}
			}
			form += ">";
			formList.add(form);
		} else if(formTag && t == HTML.Tag.SELECT)
		{
			selectTag = true;
			selectInfo = new String("<select");
			Enumeration<Attribute> attributeNames = (Enumeration<Attribute>)a.getAttributeNames();
			if(attributeNames != null)
			{
				for(;attributeNames.hasMoreElements();)
				{
					Object attributeName = attributeNames.nextElement();
					if(attributeName.getClass() == String.class)
					{
						selectInfo += " " + attributeName + "=\"" + a.getAttribute(attributeName) + "\"";
					} else if(attributeName.getClass() == Attribute.class)
					{
						selectInfo +=  " " + attributeName.toString() + "=\"" + a.getAttribute(attributeName) + "\"";
					}
				}
			} 
			selectInfo += " >";
		} else if(formTag && selectTag && t == HTML.Tag.OPTION)
		{
			selectInfo += "\n   <option";
			Enumeration<Attribute> attributeNames = (Enumeration<Attribute>)a.getAttributeNames();
			if(attributeNames != null)
			{
				for(;attributeNames.hasMoreElements();)
				{
					Object attributeName = attributeNames.nextElement();
					if(attributeName.getClass() == String.class)
					{
						selectInfo += " " + attributeName + "=\"" + a.getAttribute(attributeName) + "\"";
					} else if(attributeName.getClass() == Attribute.class)
					{
						selectInfo +=  " " + attributeName.toString() + "=\"" + a.getAttribute(attributeName) + "\"";
					}
				}
			}
			selectInfo += " >";
		}
		
	}

	@Override
	public void handleText(char[] data, int pos) {
		labelInfo = new String(data);
		if(formTag && selectTag)
		{
			selectInfo += labelInfo;
		} else if (formTag && !selectTag) 
		{
			if(labelInfo != null && labelInfo.length() > 0)
				inputList.add(labelInfo);
		}
	}

	
}
	



